﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Data.SqlClient;
using Common;
using System.Data;
using System.Web.Script.Serialization;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Web.UI;
using System.Text;

namespace WebPortal.Controllers
{
    public class SysJobController : BaseController
    {
        public ActionResult JobList()
        {
            return View();
        }
        /// <summary>
        /// 搜索列表
        /// </summary>
        /// <param name="n">名称</param>
        /// <param name="t">类型</param>
        /// <param name="s">状态</param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult Sreach(string dbName, string n, string t, string s, int rows = 0, int page = 1)
        {
            string order = Request.Params["order"];
            string sort = Request.Params["sort"];
            if (string.IsNullOrEmpty(sort))
            {
                sort = "TaskName";
            }
            if (string.IsNullOrEmpty(order))
            {
                order = "asc";
            }
            IDataParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@TaskName", n);
            paras[1] = new SqlParameter("@TaskType", t);
            paras[2] = new SqlParameter("@TaskStatus", s);
            paras[3] = new SqlParameter("@Rows", rows);
            paras[4] = new SqlParameter("@PageNum", page);
            paras[5] = new SqlParameter("@Sort", sort);
            paras[6] = new SqlParameter("@Order", order);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_TaskInfo> list = etlTaskBLL.GetList(paras);//数据列表
            int count = etlTaskBLL.GetListCount(paras);//总记录数
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);

            return Json(jsonDict);
        }
        /// <summary>
        /// 初始化列表
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult List(string dbName, int rows, int page)
        {
            IDataParameter[] paras = new SqlParameter[5];
            paras[0] = new SqlParameter("@TaskName", "");
            paras[1] = new SqlParameter("@TaskType", "");
            paras[2] = new SqlParameter("@TaskStatus", "");
            paras[3] = new SqlParameter("@Rows", rows);
            paras[4] = new SqlParameter("@PageNum", page);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_TaskInfo> list = etlTaskBLL.GetList(paras);//数据列表
            int count = etlTaskBLL.GetListCount(paras);//总记录数
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 添加或修改任务页面初始化
        /// </summary>
        /// <param name="id">任务id</param>
        /// <returns></returns>
        public ActionResult GetJob(string dbName, string id)
        {
            if (id == null)//添加页面初始化
            {
                ETL_TaskInfo info = new Model.ETL_TaskInfo();
                info.TaskIntervalDays = 1;
                info.TaskStartDate = DateTime.Now.ToString();//.ToString("yyyy-MM-dd");
                info.TaskEndDate = DateTime.Now.AddYears(100).ToString();//.ToString("yyyy-MM-dd");
                return View(info);
            }
            else//修改页面初始化
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
                ETL_TaskInfo model = etlTaskBLL.GetInfoById(id);
                return View(model);
            }
        }
        ///// <summary>
        ///// 立即执行动作
        ///// </summary>
        ///// <param name="id">任务id</param>
        ///// <returns></returns>
        //public ActionResult DoTask(string dbName, string id)
        //{
        //    LoginInfo loginInfos = base.loginInfo();
        //    loginInfos.DBName = dbName;
        //    ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
        //    if (etlTaskBLL.DoTask(id))
        //    {
        //        new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.添加, OperationStatus.成功, "立即执行任务操作,操作成功,ID=" + id, "superadmin");
        //        return Json("操作成功！");
        //    }
        //    else
        //    {
        //        new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.添加, OperationStatus.失败, "立即执行任务操作,操作失败,ID=" + id, "superadmin");
        //        return Json("操作失败！");
        //    }
        //}
        /// <summary>
        /// 已保存任务明细
        /// </summary>
        /// <param name="id">任务id</param>
        /// <returns></returns>
        public string GetDetail(string dbName, string id)
        {
            IDataParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@taskID", id);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_Task_Details> list = etlTaskBLL.GetDetails(paras);//已保存任务明细
            System.Text.StringBuilder html = new System.Text.StringBuilder();
            foreach (ETL_Task_Details model in list)
            {
                html.Append(string.Format("<div class='{0}' name='{2}' onclick=\"CKright('{0}');\">{1}</div>", model.JobStepID, model.StepName, model.StepType));
            }
            return html.ToString();
        }
        /// <summary>
        /// 初始化任务明细
        /// </summary>
        /// <param name="id">任务id</param>
        /// <returns></returns>
        public string GetStep(string dbName, string id)
        {
            IDataParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@taskID", id);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_Task_Details> list_Details = etlTaskBLL.GetDetails(paras);//已保存任务明细
            DataTable dt = etlTaskBLL.GetStep();//所有任务明细
            dt.DefaultView.Sort = "JobOrderID asc";
            System.Text.StringBuilder html = new System.Text.StringBuilder();
            bool td = true;
            bool gd = true;
            foreach (ETL_Task_Details model in list_Details)//排除已保存任务明细中ALL类型
            {
                if (model.JobStepID == "ALL" && model.StepType == "T")
                    td = false;
                if (model.JobStepID == "ALL" && model.StepType == "L")
                    gd = false;
            }
            foreach (DataRow dr in dt.Rows)
            {
                string stID = dr["StepID"].ToString().ToLower();
                string st = " style='display:none;'";
                if (list_Details.FindIndex(o => { return o.JobStepID.ToLower() == stID; }) == -1)//排除已保存任务明细
                {
                    st = "";
                }
                if (dr["JobOrderID"].ToString() == "3" && td)
                {
                    td = false;
                    html.Append(string.Format("<div class=\"ALL\" name='T' onclick=\"CKleft('T','');\" " + st + ">所有加工数据</div>"));
                }
                if (dr["JobOrderID"].ToString() == "99" && gd)
                {
                    gd = false;
                    html.Append(string.Format("<div class=\"ALL\" name='L' onclick=\"CKleft('L','');\"" + st + ">所有视图组</div>"));
                }
                html.Append(string.Format("<div class=\"{0}\" name='{2}' id='{3}' onclick=\"CKleft('{0}','{2}');\" {4}>&nbsp;&nbsp;{1}</div>", dr["StepID"], dr["StepName"], dr["StepType"], dr["JobOrderID"], st));

            }
            return html.ToString();
        }
        /// <summary>
        /// 保存任务
        /// </summary>
        /// <param name="collection">提交数据</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveItem(FormCollection collection, string dbName)
        {
            Dictionary<string, string> query = collection["query"].FromJSon<Dictionary<string, string>>();
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            try
            {
                ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
                IDataParameter[] paras = new SqlParameter[query.Count - 1];
                int index = 0;
                string newid = Guid.NewGuid().ToString();
                string[] arr_IDs = { };
                foreach (string item in query.Keys)//解析提交数据
                {
                    switch (item)
                    {
                        case "JobStepID":
                            arr_IDs = query[item].Split('&');
                            paras[index] = new SqlParameter("@" + item, "");
                            break;
                        case "TaskID":
                            if (query[item].HasValue())
                                newid = query[item];
                            else
                            {
                                string taskName = query["TaskName"];
                                if (etlTaskBLL.CheckName(taskName))
                                    return Json("操作失败！存在相同的任务名称！");
                            }
                            paras[index] = new SqlParameter("@" + item, newid);
                            break;
                        case "FType":
                            index -= 1;
                            break;
                        default:
                            paras[index] = new SqlParameter("@" + item, query[item]);
                            break;
                    }
                    index += 1;
                }
                if (etlTaskBLL.ClearStep(newid))//清除已保存明细
                {
                    foreach (string sid in arr_IDs)
                    {
                        if (!string.IsNullOrEmpty(sid))
                        {
                            string[] st = sid.Split(',');
                            IDataParameter[] paras_ = new SqlParameter[4];
                            paras_[0] = new SqlParameter("@TaskID", newid);
                            paras_[1] = new SqlParameter("@JobStepID", st[0]);
                            paras_[2] = new SqlParameter("@StepType", st[1]);
                            paras_[3] = new SqlParameter("@StepOrder", st[2]);
                            etlTaskBLL.UpdateStep(paras_);//保存明细
                        }
                    }
                    string msg = etlTaskBLL.Update(paras);//保存任务
                    new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.成功, "更新任务操作,操作成功,ID=" + newid, "superadmin");
                    return Json(msg);
                }
                else
                {
                    new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.失败, "更新任务明细操作,操作失败,ID=" + newid, "superadmin");
                    return Json("操作失败！更新任务明细失败！");
                }
            }
            catch (Exception ex)
            {
                new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.失败, "更新任务操作,操作失败：" + ex.Message, "superadmin");
                return Json(ex.Message);
            }
        }
        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="id">任务id</param>
        /// <param name="status"></param>
        public void UpStatus(string dbName, string id, int status)
        {
            IDataParameter[] paras = new SqlParameter[16];
            paras[0] = new SqlParameter("@TaskID", id);
            paras[1] = new SqlParameter("@TaskName", "");
            paras[2] = new SqlParameter("@TaskType", "");
            paras[3] = new SqlParameter("@UsedMailReport", "");
            paras[4] = new SqlParameter("@UsedSMSReport", "");
            paras[5] = new SqlParameter("@UsedRTXReport", "");
            paras[6] = new SqlParameter("@TaskIntervalDays", "");
            paras[7] = new SqlParameter("@FrequencyType", "");
            paras[8] = new SqlParameter("@TaskStartTime", "");
            paras[9] = new SqlParameter("@TaskEndTime", "");
            paras[10] = new SqlParameter("@TaskEveryDayIntervalMinute", "");
            paras[11] = new SqlParameter("@TaskStartDate", "");
            paras[12] = new SqlParameter("@TaskEndDate", "");
            paras[13] = new SqlParameter("@TaskStatus", status);
            paras[14] = new SqlParameter("@JobStepID", "");
            paras[15] = new SqlParameter("@TaskDescription", "");
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            etlTaskBLL.Update(paras);
            new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.成功, "更新任务状态操作,操作成功,ID=" + id + ",Status=" + status, "superadmin");
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id">任务id</param>
        /// <returns></returns>
        public string DeleteItem(string dbName, string id)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            string msg = etlTaskBLL.Delete(id);
            new SysOperationLog(loginInfos).Log("作业任务管理", Request.UserHostAddress, OperationAction.删除, OperationStatus.成功, "删除任务操作,操作成功", "superadmin");
            return msg;
        }
        /// <summary>
        /// 取任务日志列表
        /// </summary>
        /// <param name="id">任务id</param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult GetTaskLog(string dbName, string id, int rows, int page)
        {
            IDataParameter[] paras = new SqlParameter[3];
            paras[0] = new SqlParameter("@ID", id);
            paras[1] = new SqlParameter("@rows", rows);
            paras[2] = new SqlParameter("@page", page);
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_Task_Details_HistoryLog> list = etlTaskBLL.GetTaskLog(paras);//取任务日志列表
            int count = 0;
            if (list.Count > 0)
                count = list[0].Count;
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);
            return Json(jsonDict);
        }
        /// <summary>
        /// 取任务日志信息
        /// </summary>
        /// <param name="id">日志id</param>
        /// <returns></returns>
        public ActionResult GetTaskLogItem(string dbName, string id)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            List<ETL_Task_Details_HistoryLog> list = etlTaskBLL.GetTaskLogItem(id);//取任务日志信息
            ETL_Task_Details_HistoryLog model = new ETL_Task_Details_HistoryLog();
            if (list.Count > 0)
                model = list[0];
            return Json(model);
        }
        /// <summary>
        /// 取任务执行计划表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTaskRunStatus(string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            DataTable dt = etlTaskBLL.TaskRunStatus("", 1, 0);//取任务执行计划表
            DataTable dtClone = dt.Clone();
            for (int j = 4; j < dt.Columns.Count; j++)
            {
                dtClone.Columns[j].DataType = typeof(string);
            }
            foreach (DataRow dr in dt.Rows)
            {
                DataRow drClone = dtClone.Rows.Add();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j < 4)
                        drClone[j] = dr[j].ToString();
                    else
                    {
                        switch (dr[j].ToString())
                        {
                            case "0":
                                drClone[j] = "<div class='c0 cw'>×</div>";
                                break;
                            default:
                                drClone[j] = "<div class='c" + dr[j].ToString() + " cw'>√</div>";
                                break;
                        }
                    }
                }
            }
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", 0);
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            jsonDict.Add("rows", jsSerializer.DeserializeObject(dtClone.ToJson()));

            return Json(jsonDict);
        }
        /// <summary>
        /// 取任务执行计划表列名
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTableColumns(string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
            DataTable dt = etlTaskBLL.TaskRunStatus("", 0, 0);//取任务执行计划表列

            IList<TableColumnInfo> data = new List<TableColumnInfo>();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (i > 3)
                {
                    string title = dt.Columns[i].ColumnName;
                    data.Add(new TableColumnInfo() { ColumnName = title, ColumnDescription = title, ColumnValue = title });
                }
            }
            return Json(data);
        }
        /// <summary>
        /// 导出任务执行计划表
        /// </summary>
        /// <returns></returns>
        public ActionResult ExportData(string dbName)
        {
            try
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                ETLTaskBLL etlTaskBLL = new ETLTaskBLL(loginInfos);
                dt = etlTaskBLL.TaskRunStatus("", 1, 0);//取任务执行计划表
                //if (t == 1)
                //{
                Exphtml(dt);
                return View();
                //}
                //else
                //{
                //    ds.Tables.Add(dt);
                //    System.IO.StreamReader sr = ToExcelSheet(ds);
                //    return File(sr.BaseStream, "application/msexcel", string.Format("ExcelData_{0}.xls", DateTime.Now.ToString("ddHHmmss")));
                //}
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
        /// <summary>
        /// 转换任务执行状态
        /// </summary>
        /// <param name="v">任务状态</param>
        /// <returns></returns>
        private string fv(string v)
        {
            string value = "<div class='c" + v + " cw'>√</div>";
            switch (v)
            {
                case "0":
                    value = "<div class='c0 cw'>×</div>";
                    break;
            }
            return value;
        }
        /// <summary>
        /// 导出任务执行计划表到Excel
        /// </summary>
        /// <param name="dt">计划表数据集</param>
        private void Exphtml(DataTable dt)
        {
            //初始化导出文件头信息
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=ExportData_{0}.xls", DateTime.Now.ToString("ddHHmmss")));
            Response.ContentType = "application/vnd.ms-excel";

            //转换导出内容
            System.Text.StringBuilder txtData = new System.Text.StringBuilder();
            txtData.Append("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
            txtData.Append(@"<style>.c0{color:#000000;text-align:center;font-weight:bold;}.c1{color:#0000FF;text-align:center;font-weight:bold;}.c2{color:#ff0000;text-align:center;font-weight:bold;}.c3{color:#00FF00;text-align:center;font-weight:bold;}.c4{color:#008000;text-align:center;font-weight:bold;}</style>");
            txtData.Append("</head><body><table border='1'><tr>");
            System.Text.StringBuilder hdData = new System.Text.StringBuilder();
            //表字段行
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                string title = string.Empty;
                switch (j)
                {
                    case 0:
                        title = "<td>任务名称";
                        break;
                    case 1:
                        title = "<td>任务状态";
                        break;
                    case 2:
                        title = "<td>执行周期";
                        break;
                    case 3:
                        title = "<td>分钟间隔";
                        break;
                    default:
                        title = "<td style='width:45px'>" + dt.Columns[j].ColumnName;
                        break;
                }
                hdData.Append(title);
                hdData.Append("</td>");
            }
            txtData.Append(hdData.ToString());
            txtData.Append("</tr><tr>");
            //表内容行
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                System.Text.StringBuilder dcData = new System.Text.StringBuilder();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string value = dt.Rows[i][j].ToString();
                    switch (j)
                    {
                        case 0:
                            dcData.Append("<td>");
                            dcData.Append(value);
                            break;
                        case 1:
                            dcData.Append("<td>");
                            dcData.Append(value);
                            break;
                        case 2:
                            dcData.Append("<td>");
                            dcData.Append(value);
                            break;
                        case 3:
                            dcData.Append("<td>");
                            dcData.Append(value); 
                            break;
                        default:
                            if (value == "0")
                                dcData.Append("<td class='c0'>×");
                            else
                            {
                                dcData.Append("<td class='c");
                                dcData.Append(value);
                                dcData.Append("'>√");
                            }
                            break;
                    }
                    dcData.Append("</td>");
                }
                txtData.Append(dcData.ToString());
                txtData.Append("</tr>");
            }
            txtData.Append(@"</table><font>&nbsp;</font><table border='1'><tr><td colspan='2'>说明：</td></tr><tr><td><font class='c0'>×</font></td><td><font>不执行。</font></td></tr><tr><td><font class='c1'>√</font></td><td><font>N分钟间隔并且状态为启用。</font></td></tr><tr><td>
<font class='c2'>√</font></td><td><font>N分钟间隔并且状态为禁用。</font></td></tr><tr><td><font class='c3'>√</font></td><td><font>N天1次并且状态为启用。 </font></td></tr><tr><td><font class='c4'>√</font></td><td><font>N天1次并且状态为禁用。</font></td></tr></table></body></html>");
            this.HttpContext.Response.Write(txtData.ToString());
            this.HttpContext.Response.End();
        }
        /// <summary>
        /// 导出任务执行计划表到Excel：Worksheet方法（失效）
        /// </summary>
        /// <param name="dt">计划表数据集</param>
        private StreamReader ToExcelSheet(DataSet ds)
        {
            int testnum = ds.Tables.Count - 1;

            Excel.Application appExcel;
            appExcel = new Excel.Application();

            Excel.Workbook workbookData;
            Excel.Worksheet worksheetData;

            workbookData = appExcel.Workbooks.Add(Missing.Value);
            //
            //workbookData.Worksheets.Delete();
            for (int k = 0; k < ds.Tables.Count; k++)
            {
                worksheetData = (Excel.Worksheet)workbookData.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                worksheetData.Name = "Sheet_" + testnum.ToString();
                testnum--;
                if (ds.Tables[k] != null)
                {
                    for (int j = 0; j < ds.Tables[k].Columns.Count; j++)
                    {
                        string title = string.Empty;
                        switch (j)
                        {
                            case 0:
                                title = "任务名称";
                                break;
                            case 1:
                                title = "任务状态";
                                break;
                            case 2:
                                title = "按日间隔";
                                break;
                            case 3:
                                title = "分钟间隔";
                                break;
                            default:
                                title = ds.Tables[k].Columns[j].ColumnName;
                                break;

                        }
                        worksheetData.Cells[1, j + 1] = title;
                    }
                    for (int i = 0; i < ds.Tables[k].Rows.Count; i++)
                    {
                        for (int j = 0; j < ds.Tables[k].Columns.Count; j++)
                        {
                            string value = ds.Tables[k].Rows[i][j].ToString();
                            switch (j)
                            {
                                case 0:
                                    worksheetData.Cells[i + 2, j + 1] = value;
                                    break;
                                case 1:
                                    if (value == "0")
                                        worksheetData.Cells[i + 2, j + 1] = "禁用";
                                    else
                                        worksheetData.Cells[i + 2, j + 1] = "启用";
                                    break;
                                case 2:
                                    worksheetData.Cells[i + 2, j + 1] = value + "天";
                                    break;
                                case 3:
                                    worksheetData.Cells[i + 2, j + 1] = value + "分钟";
                                    break;
                                default:
                                    Excel.Range rng = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                    switch (value)
                                    {
                                        case "0":
                                            //worksheetData.Cells[i + 2, j + 1] = "×";
                                            //Excel.Range rng1 = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                            //rng1.Font.ColorIndex = 1;
                                            rng.Value = "×";
                                            rng.Font.ColorIndex = 1;
                                            break;
                                        case "1":
                                            //worksheetData.Cells[i + 2, j + 1] = "√";
                                            //Excel.Range rng5 = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                            //rng5.Font.ColorIndex = 5;
                                            rng.Value = "√";
                                            rng.Font.ColorIndex = 5;
                                            break;
                                        case "2":
                                            //worksheetData.Cells[i + 2, j + 1] = "√";
                                            //Excel.Range rng3 = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                            //rng3.Font.ColorIndex = 3;
                                            rng.Value = "√";
                                            rng.Font.ColorIndex = 3;
                                            break;
                                        case "3":
                                            //worksheetData.Cells[i + 2, j + 1] = "√";
                                            //Excel.Range rng4 = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                            //rng4.Font.ColorIndex = 4;
                                            rng.Value = "√";
                                            rng.Font.ColorIndex = 4;
                                            break;
                                        case "4":
                                            //worksheetData.Cells[i + 2, j + 1] = "√";
                                            //Excel.Range rng10 = (Excel.Range)worksheetData.Cells[i + 2, j + 1];
                                            //rng10.Font.ColorIndex = 10;
                                            rng.Value = "√";
                                            rng.Font.ColorIndex = 10;
                                            break;
                                    }
                                    break;
                            }
                        }
                    }
                }

                worksheetData.Columns.EntireColumn.AutoFit();
                workbookData.Saved = true;

            }
            string sheetName = Guid.NewGuid().ToString().Replace("-", "") + ".xls";
            string filePath = HttpContext.Server.MapPath("/ExportData/");
            string strFileName = filePath + sheetName;
            //int FormatNum = 56;
            //if (Convert.ToDouble(appExcel.Version) < 12)
            //    FormatNum = -4143;
            workbookData.SaveAs(@strFileName);
            workbookData.Close();
            appExcel.Quit();
            System.IO.StreamReader sr = new StreamReader(strFileName);
            string[] files = System.IO.Directory.GetFiles(filePath);
            foreach (string s in files)
            {
                DateTime fileDate = System.IO.File.GetLastWriteTime(s);
                TimeSpan ts = DateTime.Now - fileDate;
                if (ts.Days > 0)
                    System.IO.File.Delete(s);
            }
            return sr;
        }
    }
}
